package controlLayer;
import modelLayer.*;

/**
 * Controls dvds
 * 
 * @author Gruppe E
 * @version 1.0
 */
public class DVDController
{
    private DVDContainer dCon;
    private DVD dvd;

    /**
     * Constructor for objects of class DVDController
     */
    public DVDController()
    {
        dCon = dCon.getInstance();
    }

    /**
     * Method createDVD
     * 
     * creates a DVD and adds it to the ArrayList dvds in DVDController
     *
     * @param barcode A parameter
     * @param title A parameter
     * @param artist A parameter
     * @param publicationDate A parameter
     * @return The return value
     */
    public boolean createDVD(String barcode, String title, String artist, String publicationDate)
    {
        DVD tempD = null;
        if(findDVD(barcode) == null)
        {
            tempD = new DVD(barcode, title, artist, publicationDate);
            dCon.addDVD(tempD);
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * Method findDVD
     * 
     * searches for a DVD from the given barcode
     *
     * @param barcode A parameter
     * @return The return value
     */
    public DVD findDVD(String barcode)
    {
        return dCon.findDVD(barcode);
    }

    /**
     * Method updateDVD
     * 
     * searches for the DVDs barcode, and updates the DVDs information
     *
     * @param oldBarcode A parameter
     * @param title A parameter
     * @param artist A parameter
     * @param publicationDate A parameter
     * @param newBarcode A parameter
     * @return The return value
     */
    public boolean updateDVD(String oldBarcode, String title, String artist, String publicationDate, String newBarcode)
    {
        if(findDVD(oldBarcode) == null)
        {
            return false;
        }
        else
        {
            return dCon.updateDVD(oldBarcode, title, artist, publicationDate, newBarcode);
        }
    }

    /**
     * Method deleteDVD
     * 
     * deletes a DVD that matches the given barcode
     *
     * @param barcode A parameter
     * @return The return value
     */
    public boolean deleteDVD(String barcode)
    {
        return dCon.deleteDVD(barcode);
    }

    /**
     * Method createCopy
     * 
     * creates a copy and adds it to the ArrayList copies in DVD
     *
     * @param barcode A parameter
     * @param serialNumber A parameter
     * @param purchaseDate A parameter
     * @param purchasePrice A parameter
     * @return "invalidBarcode" for invalid barcode, "confirmed" for copy created, "copyExists" for copy exists
     */
    public String createCopy(String barcode, String serialNumber, String purchaseDate, int purchasePrice)
    {
        if(findDVD(barcode)==null)
        {
            return "invalidBarcode";
        }
        else
        {
            Copy tempC = null;
            DVD tempD = findDVD(barcode);
            if(findCopy(barcode, serialNumber) == null)
            {
                tempC = new Copy(serialNumber, purchaseDate, purchasePrice, tempD);
                tempD.addCopy(tempC);
                return "confirmed";

            }
            else
            {
                return "copyExists";
            }
        }
    }

    /**
     * Method findCopy
     * 
     * searches for a Copy from the given barcode and serial number
     *
     * @param barcode A parameter
     * @param serialNumber A parameter
     * @return The return value
     */
    public Copy findCopy(String barcode, String serialNumber)
    {
        DVD tempD = findDVD(barcode);
        if(tempD==null)
        {
            return null;
        }
        else
        {
            return tempD.findCopy(serialNumber);
        }
    }

    /**
     * Method updateCopy
     * 
     * searches for the copy's barcode, and updates the copy's information
     *
     * @param barcode A parameter
     * @param oldSerialNumber A parameter
     * @param newSerialNumber A parameter
     * @param purchaseDate A parameter
     * @param purchasePrice A parameter
     * @return The return value
     */
    public boolean updateCopy(String barcode, String oldSerialNumber, String newSerialNumber, String purchaseDate, int purchasePrice, boolean state)
    {
        DVD tempD = findDVD(barcode);
        if(findCopy(barcode, oldSerialNumber) == null)
        {
            return false;
        }
        else
        {
            return tempD.updateCopy(oldSerialNumber, purchaseDate, purchasePrice, newSerialNumber, state);
        }
    }

    /**
     * Method deleteCopy
     * 
     * deletes a copy that matches the given barcode and serial number
     *
     * @param barcode A parameter
     * @param serialNumber A parameter
     * @return The return value
     */
    public boolean deleteCopy(String barcode, String serialNumber)
    {
        DVD tempD = findDVD(barcode);
        if(findCopy(barcode, serialNumber) == null)
        {
            return false;
        }
        else
        {
            return tempD.deleteCopy(serialNumber);
        }
    }

    /**
     * Method isHome
     *
     * @param barcode A parameter
     * @param serialNumber A parameter
     * @return boolean isHome
     */
    public boolean isHome(String barcode, String serialNumber)
    {
        Copy tempC = findCopy(barcode, serialNumber);
        return tempC.getState();
    }
}
